import styles from './index.module.scss'
import NavBar from '@/components/Icon/Narbar'
import Input from '../../components/Input'
import { useFormik } from 'formik'
import * as Yup from 'yup'
import { Toast } from 'antd-mobile'
import classnames from 'classnames'
import { useDispatch } from 'react-redux'
import { sendCode, login } from '@/store/action/login'
import { useState } from 'react'
import { useHistory, useLocation } from 'react-router'
// import { NavBar } from 'antd-mobile'
export default function Login() {
  const location = useLocation()
  const history = useHistory()
  const [time, setTime] = useState(0)
  const formik = useFormik({
    initialValues: {
      mobile: '13999999999',
      code: '246810',
    },
    validationSchema: Yup.object().shape({
      // 手机号验证规则
      mobile: Yup.string()
        .required('请输入手机号')
        .matches(/^1[3456789]\d{9}$/, '手机号格式错误'),

      // 手机验证码验证规则
      code: Yup.string()
        .required('请输入验证码')
        .matches(/^\d{6}$/, '验证码6个数字'),
    }),
    async onSubmit(values) {
      await dispatch(login(values))
      Toast.success('登陆成功')
      if (location.from) {
        history.push(location.from.pathname)
      } else {
        history.replace('/home')
      }
    },
  })
  const {
    values: { mobile, code },
    handleChange,
    handleSubmit,
    handleBlur,
    errors,
    touched,
    isValid,
  } = formik
  const dispatch = useDispatch()
  const onExtraClick = async () => {
    if (time > 0) return
    console.log(1)
    if (!/^1[3456789]\d{9}$/.test(mobile)) {
      formik.setTouched({
        mobile: true,
      })
      return
    }

    await dispatch(sendCode(mobile))
    Toast.success('获取验证码成功', 1)
    setTime(5)
    let times = setInterval(() => {
      setTime((time) => {
        if (time === 1) {
          clearInterval(times)
        }
        return time - 1
      })
    }, 1000)
  }

  return (
    <div className={styles.root}>
      <NavBar>登录</NavBar>
      <div className="content">
        {/* 标题 */}
        <h3>短信登录</h3>
        <form onSubmit={handleSubmit}>
          {/* 手机号输入框 */}
          <div className="input-item">
            <div className="input-box">
              <Input
                className="input"
                name="mobile"
                maxLength={11}
                placeholder="请输入手机号"
                autoComplete="off"
                value={mobile}
                onChange={handleChange}
                onBlur={handleBlur}
              />
            </div>
            {touched.mobile && errors.mobile ? (
              <div className="validate">手机号验证错误信息</div>
            ) : null}
          </div>
          {/* 短信验证码输入框 */}
          <div className="input-item">
            <div className="input-box">
              <Input
                className="input"
                name="code"
                placeholder="请输入验证码"
                maxLength={6}
                autoComplete="off"
                onChange={handleChange}
                onExtraClick={onExtraClick}
                value={code}
                onBlur={handleBlur}
                extra={time === 0 ? '获取验证码' : time + 's后获取'}
              />
              {/* <div className="extra">获取验证码</div> */}
            </div>
            {touched.code && errors.code ? (
              <div className="validate">验证码错误</div>
            ) : null}
          </div>

          {/* 登录按钮 */}
          <button
            type="submit"
            className={classnames('login-btn')}
            disabled={!isValid}
          >
            登录
          </button>
        </form>
      </div>
    </div>
  )
}
